package 富婆梦.第一阶段.第七周IO流;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.channels.FileLock;
import java.util.concurrent.TimeUnit;

/**
 * @ProjectName: java-learning
 * @Package: 富婆梦.第一阶段.第七周
 * @ClassName: FileLocking
 * @Author: hemengjin
 * @Description: 文件加锁
 * @Date:2021/9/12 23:30
 * @Version: 1.0
 */
public class FileLocking {
    public static void main(String[] args) throws Exception {
        File file = new File("D:/何梦锦的宝藏/Java学习/Echo.txt");
        FileOutputStream fos = new FileOutputStream(file);
        FileLock fl = fos.getChannel().tryLock();
        if (fl != null) {
            System.out.println("Lock File!");
            TimeUnit.MILLISECONDS.sleep(100);
            fl.release();
            System.out.println("Release Lock");
        }
        fos.close();
    }
}
